package com.sansecy.mocklocationtest.loadedapk;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.didi.virtualapk.PluginManager;
import com.sansecy.mocklocationtest.AppInfo;
import com.sansecy.mocklocationtest.R;
import com.sansecy.mocklocationtest.TargetActivity;

import java.io.File;
import java.util.List;

public class loadedAppListAdapter extends RecyclerView.Adapter<loadedAppListAdapter.AppListHolder> {

    private List<AppInfo> mInstalledPackages;
    private Context mContext;

    public loadedAppListAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public AppListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_app, parent, false);
        return new AppListHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AppListHolder holder, int position) {
        AppInfo appInfo = mInstalledPackages.get(position);
        holder.imageView.setImageDrawable(appInfo.getIcon());
        holder.textView.setText(appInfo.getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                String pluginPath = Environment.getExternalStorageDirectory().getAbsolutePath().concat("/Test.apk");
                File plugin = new File(appInfo.getAPK());
                try {
                    PluginManager.getInstance(mContext.getApplicationContext()).loadPlugin(plugin);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // Given "com.didi.virtualapk.demo" is the package name of plugin APK,
                // and there is an activity called `MainActivity`.
                Intent intent = new Intent();
                intent.setClassName("com.huawei.welink", "huawei.w3.ui.welcome.W3SplashScreenActivity");
                mContext.startActivity(intent);
//                mContext.startActivity(new Intent(mContext, TargetActivity.class));
            }
        });
    }

    private static final String TAG = "AppListAdapter-App";

    @Override
    public int getItemCount() {
        return mInstalledPackages == null ? 0 : mInstalledPackages.size();
    }

    public void setData(List<AppInfo> installedPackages) {
        mInstalledPackages = installedPackages;
        notifyDataSetChanged();
    }

    static class AppListHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        public AppListHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image);
            textView = itemView.findViewById(R.id.text);
        }
    }

}
